package com.Tools.Calligraphy
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	
	public class Brush extends Sprite
	{
		[Embed(source="com/Tools/Calligraphy/Brush/Brush.png")]
		private var brush:Class;
		
		private var content:Sprite;//画好的容器
		
		private var board:Sprite;//黑板
		
		private var brush_pen:Sprite;//移动的笔刷
		
		private var view_data:BitmapData;//显示容器
		
		private var view:Bitmap;//显示容器
		
		private var last_point:Point;//存储上次的位置
		
		private var oldScale:Number;//存储上次的缩放大小
		
		private var brush_mc:Sprite;//笔刷集合
		
		private var defaultScale:Number = .8;//默认笔触的大小
		
		private var cx:Number = .02;//粗细变化参数
		
		private var brushMin:Number = .08;//最细笔触限制
		
		private var brushAlpha:Number = .3;//笔刷浓度
		
		private var brushBlur:Number = 1;//笔刷模糊
		
		private var midu:Number = 1;//笔刷密度
		
		private var bf:BlurFilter;
		
		
		public function Brush(_width:Number=200,_height:Number=100) {
			
			//设置显示器
			view_data = new BitmapData(_width, _height,true,0x00ffffff);
			view = new Bitmap(view_data);
			view.smoothing = true;
			addChild(view);
			
			
			//设置绘图板
			content = new Sprite();
			addChild(content);
			
			//设置黑板
			board=new Sprite();
			board.graphics.beginFill(0x000000,0.01);
			board.graphics.drawRect(0,0,_width,_height);
			board.graphics.endFill();
			addChild(board);
			
			//设置笔刷
			var temp_brush:Bitmap=new brush();
			brush_pen = new Sprite();
			brush_pen.addChild(temp_brush);
			brush_pen.alpha = brushAlpha;
			brush_pen.scaleX = brush_pen.scaleY = defaultScale;
			bf = new BlurFilter(brushBlur, brushBlur);//滤镜
			brush_pen.filters = new Array(bf);
			brush_pen.visible = false;

			//笔刷集合
			brush_mc = new Sprite();
			
			content.addChild(brush_pen);
			content.addChild(brush_mc);
			
			//黑板监听
			board.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
			board.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
		}
		
		//鼠标按下
		private function _mouseDown(e:MouseEvent):void
		{
			last_point=new Point(mouseX,mouseY);
			
			brush_pen.x = last_point.x;
			brush_pen.y = last_point.y;
			oldScale = 1;
			
			brush_pen.startDrag(true);
			brush_pen.visible = true;
			
			board.addEventListener(MouseEvent.MOUSE_MOVE, _addBrush);
		}
		
		//松开鼠标
		private function _mouseUp(e:MouseEvent):void
		{
			brush_pen.visible = false;
			brush_pen.scaleX = brush_pen.scaleY = 1;
			brush_pen.stopDrag();
			
			board.removeEventListener(MouseEvent.MOUSE_MOVE, _addBrush);
		}
		

		private function _addBrush(e:Event):void
		{
			var new_point:Point=new Point(mouseX,mouseY);
			
			var dis:Number=Point.distance(new_point,last_point);
			
			//改变笔触的大小,越快越小
			if (dis > 0) {
				var scale:Number = defaultScale - dis * cx;
				if (scale > 1) {
					scale = 1;
				}else if (scale < brushMin) {
					scale = brushMin;
				}
				scale = (oldScale + scale) / 2;
				brush_pen.scaleY = brush_pen.scaleX = scale;
			}
			
			//填充
			var count:int = Math.floor(dis / midu);
			var scaleBili:Number = (oldScale-scale) / count;
			for (var i:int=0; i<count-1; i++) {
				var brush1:Bitmap = new brush();
				brush1.filters = new Array(bf);
				brush1.alpha = brushAlpha;
				brush1.scaleX = brush1.scaleY = oldScale-i * scaleBili;
				brush_mc.addChild(brush1);
				brush1.x=((new_point.x-last_point.x)/count)*(i+1)+last_point.x;
				brush1.y=((new_point.y-last_point.y)/count)*(i+1)+last_point.y;
			}
			oldScale = scale;
			last_point.x = mouseX;
			last_point.y = mouseY;
			view_data.draw(content);
			//删除填充
			while (brush_mc.numChildren>0) {
				brush_mc.removeChildAt(0);
			}
		}
	}
}